<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
            width: 400px;
            padding: 40px;
            animation: fadeIn 0.5s ease-in-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .login-header p {
            color: #777;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-size: 14px;
            font-weight: 600;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: all 0.3s;
        }

        .form-group input:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
            outline: none;
        }

        .form-group .input-icon {
            position: absolute;
            right: 15px;
            top: 40px;
            color: #999;
        }

        .remember-forgot {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .remember-me {
            display: flex;
            align-items: center;
        }

        .remember-me input {
            margin-right: 5px;
        }

        .forgot-password a {
            color: #667eea;
            text-decoration: none;
        }

        .login-button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, #667eea, #764ba2);
            border: none;
            border-radius: 5px;
            color: white;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }

        .login-button:hover {
            background: linear-gradient(to right, #5a6fd1, #6a4299);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .login-footer {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
            color: #777;
        }

        .login-footer a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
        }

        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }

        .success-message {
            color: #2ecc71;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-header">
        <h1>欢迎回来</h1>
        <p>请输入您的用户名和密码登录系统</p>
    </div>

    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
            <div class="error-message" id="username-error"></div>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
            <div class="error-message" id="password-error"></div>
        </div>

        <div class="remember-forgot">
            <div class="remember-me">
                <input type="checkbox" id="remember">
                <label for="remember">记住我</label>
            </div>
            <div class="forgot-password">
                <a href="#">忘记密码?</a>
            </div>
        </div>

        <button type="submit" class="login-button">登录</button>

        <div class="error-message" id="login-error"></div>
        <div class="success-message" id="login-success"></div>
    </form>

    <div class="login-footer">
        还没有账号? <a href="#">立即注册</a>
    </div>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取表单数据
        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();

        // 清除之前的错误/成功消息
        document.getElementById('login-error').style.display = 'none';
        document.getElementById('login-success').style.display = 'none';

        // 简单验证
        if (!username) {
            showError('username-error', '请输入用户名');
            return;
        }

        if (!password) {
            showError('password-error', '请输入密码');
            return;
        }

        // 发送登录请求
        login(username, password);
    });

    function showError(elementId, message) {
        const element = document.getElementById(elementId);
        element.textContent = message;
        element.style.display = 'block';
    }

    function login(username, password) {
        // 显示加载状态
        const loginButton = document.querySelector('.login-button');
        const originalText = loginButton.textContent;
        loginButton.textContent = '登录中...';
        loginButton.disabled = true;

        // 准备请求数据
        const requestData = {
            username: username,
            password: password
        };

        // 发送POST请求
        fetch('http://localhost:8080/v1/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(requestData)
        })
            .then(response => response.json())
            .then(data => {
                // 恢复按钮状态
                loginButton.textContent = originalText;
                loginButton.disabled = false;

                if (data.state === 2000) {
                    // 登录成功
                    document.getElementById('login-success').textContent = data.data;
                    document.getElementById('login-success').style.display = 'block';

                    // 这里可以添加登录成功后的跳转逻辑
                    // window.location.href = '/dashboard.html';
                } else {
                    // 登录失败
                    document.getElementById('login-error').textContent = data.data;
                    document.getElementById('login-error').style.display = 'block';
                }
            })
            .catch(error => {
                // 恢复按钮状态
                loginButton.textContent = originalText;
                loginButton.disabled = false;

                console.error('Error:', error);
                document.getElementById('login-error').textContent = '网络错误，请稍后重试';
                document.getElementById('login-error').style.display = 'block';
            });
    }

    // 输入时清除错误消息
    document.getElementById('username').addEventListener('input', function() {
        document.getElementById('username-error').style.display = 'none';
    });

    document.getElementById('password').addEventListener('input', function() {
        document.getElementById('password-error').style.display = 'none';
    });
</script>
</body>
</html>